package com.itzxx.huazhun.ui.collection.read

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.itzxx.huazhun.R
import com.itzxx.huazhun.data.model.DevicesChildBean
import com.itzxx.huazhun.data.model.ProbeStateInfo
import com.itzxx.huazhun.databinding.ActivityReadCollectionDataBinding
import com.itzxx.huazhun.ui.collection.readall.ReadCollectionAllDataActivity
import com.itzxx.huazhun.ui.record.detail.RecordDetailActivity
import com.itzxx.huazhun.ui.testing.newbuild.TestingNewBuildActivity
import com.itzxx.huazhun.ui.testing.newbuild.TestingnewBuildProbeAdapter
import com.itzxx.library_base_ktx.base.activity.BaseVmDbActivity
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter

class ReadCollectionDataActivity : BaseVmDbActivity<ReadCollectionDataViewModel,ActivityReadCollectionDataBinding>() {

    companion object {
        fun startActivity(context: Context) = context.startActivity(
            Intent(
                context,
                ReadCollectionDataActivity()::class.java
            )
        )
    }

    private lateinit var readCollectionDataAdapter: ReadCollectionDataAdapter

    override fun initView(savedInstanceState: Bundle?) {
        setSupportActionBar(mDatabind.tbHead)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        readCollectionDataAdapter = ReadCollectionDataAdapter()
        mDatabind.rvList.addLinearAdapter(readCollectionDataAdapter)
        readCollectionDataAdapter.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position) as ProbeStateInfo
            ReadCollectionAllDataActivity.startActivity(this, item.probeInfo)
            finish()
        }
    }


    override fun createObserver() {
        mViewModel.run {
            probeListState.observe(this@ReadCollectionDataActivity){
                runOnUiThread {
                    readCollectionDataAdapter.submitList(it)
                }
            }
        }
    }

    override fun isApplyDefaultWaitDialog(): Boolean {
        return true
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    override fun onDestroy() {
        super.onDestroy()
        window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }
}